package com.example.work3;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button nextPage = findViewById(R.id.nextPage);
       nextPage.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               EditText userName = findViewById(R.id.userName);
               Intent intent =new Intent(MainActivity.this,MainActivity2.class);
               intent.putExtra("name",userName.getText().toString());
               intent.putExtra("selectedOption","YES");
               startActivityForResult(intent, 1);
               userName.setText("");
           }
       });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==1){
            if (resultCode == RESULT_OK) {
                String name = data.getStringExtra("name");
                String selectedOption = data.getStringExtra("selectedOption"); // 获取返回的Intent中的额外数据
                setTitle("选择了 " + selectedOption); // 在标题中显示所选择的选项
            }
        }
    }
}